/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package workstationbookerweb.fragmentos;

import Fachada.Fachada;
import Servicios.ValidacionDatosEntrada;
import Singletons.Mensajes;
import VO.AdministradorVO;
import com.sun.rave.web.ui.appbase.AbstractFragmentBean;
import com.sun.webui.jsf.component.Button;
import com.sun.webui.jsf.component.Checkbox;
import com.sun.webui.jsf.component.Label;
import com.sun.webui.jsf.component.PasswordField;
import com.sun.webui.jsf.component.TextField;
import javax.faces.FacesException;
import javax.faces.event.ValueChangeEvent;
import workstationbookerweb.SessionBean1;
import workstationbookerweb.RequestBean1;
import workstationbookerweb.ApplicationBean1;

/**
 * <p>Fragment bean that corresponds to a similarly named JSP page
 * fragment.  This class contains component definitions (and initialization
 * code) for all components that you have defined on this fragment, as well as
 * lifecycle methods and event handlers where you may add behavior
 * to respond to incoming events.</p>
 *
 * @version framentoModificarAdministradorPorAdministrador.java
 * @version Created on 31/05/2011, 09:52:43 PM
 * @author Battousai
 */

public class framentoModificarAdministradorPorAdministrador extends AbstractFragmentBean {
    // <editor-fold defaultstate="collapsed" desc="Managed Component Definition">
    private Fachada fachada;
    private AdministradorVO avo;
    /**
     * <p>Automatically managed component initialization. <strong>WARNING:</strong>
     * This method is automatically generated, so any user-specified code inserted
     * here is subject to being replaced.</p>
     */
    private void _init() throws Exception {
        Long cedula=(Long) getValue("#{sessionScope.idAdministrador}");
        fachada =Fachada.getInstance();
        avo =fachada.leerAdministrador("", cedula);
        nombreTF.setText(avo.getNombre());
        correoOtroTF.setText(avo.getOtroCorreo());
        telefonoTF.setText(avo.getTelefono());
        direccionTF.setText(avo.getDireccion());
        nuevaContrasenaPF.setDisabled(true);
        cnuevaContrasenaPF.setDisabled(true);
    }
    private TextField nombreTF = new TextField();

    public TextField getNombreTF() {
        return nombreTF;
    }

    public void setNombreTF(TextField tf) {
        this.nombreTF = tf;
    }
    private TextField correoOtroTF = new TextField();

    public TextField getCorreoOtroTF() {
        return correoOtroTF;
    }

    public void setCorreoOtroTF(TextField tf) {
        this.correoOtroTF = tf;
    }
    private TextField telefonoTF = new TextField();

    public TextField getTelefonoTF() {
        return telefonoTF;
    }

    public void setTelefonoTF(TextField tf) {
        this.telefonoTF = tf;
    }
    private TextField direccionTF = new TextField();

    public TextField getDireccionTF() {
        return direccionTF;
    }

    public void setDireccionTF(TextField tf) {
        this.direccionTF = tf;
    }
    private PasswordField contrasenaPF = new PasswordField();

    public PasswordField getContrasenaPF() {
        return contrasenaPF;
    }

    public void setContrasenaPF(PasswordField pf) {
        this.contrasenaPF = pf;
    }
    private PasswordField nuevaContrasenaPF = new PasswordField();

    public PasswordField getNuevaContrasenaPF() {
        return nuevaContrasenaPF;
    }

    public void setNuevaContrasenaPF(PasswordField pf) {
        this.nuevaContrasenaPF = pf;
    }
    private PasswordField cnuevaContrasenaPF = new PasswordField();

    public PasswordField getCnuevaContrasenaPF() {
        return cnuevaContrasenaPF;
    }

    public void setCnuevaContrasenaPF(PasswordField pf) {
        this.cnuevaContrasenaPF = pf;
    }
    private Label mensajeL = new Label();

    public Label getMensajeL() {
        return mensajeL;
    }

    public void setMensajeL(Label l) {
        this.mensajeL = l;
    }
    private Button cambiarContrasenaB = new Button();

    public Button getCambiarContrasenaB() {
        return cambiarContrasenaB;
    }

    public void setCambiarContrasenaB(Button b) {
        this.cambiarContrasenaB = b;
    }
    // </editor-fold>

    public framentoModificarAdministradorPorAdministrador() {
    }

    /**
     * <p>Callback method that is called whenever a page containing
     * this page fragment is navigated to, either directly via a URL,
     * or indirectly via page navigation.  Override this method to acquire
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void init() {
        // Perform initializations inherited from our superclass
        super.init();
        // Perform application initialization that must complete
        // *before* managed components are initialized
        // TODO - add your own initialiation code here
        
        
        // <editor-fold defaultstate="collapsed" desc="Visual-Web-managed Component Initialization">
        // Initialize automatically managed components
        // *Note* - this logic should NOT be modified
        try {
            _init();
        } catch (Exception e) {
            log("Page1 Initialization Failure", e);
            throw e instanceof FacesException ? (FacesException) e: new FacesException(e);
        }
        
        // </editor-fold>
        // Perform application initialization that must complete
        // *after* managed components are initialized
        // TODO - add your own initialization code here
    }

    /**
     * <p>Callback method that is called after rendering is completed for
     * this request, if <code>init()</code> was called.  Override this
     * method to release resources acquired in the <code>init()</code>
     * resources that will be needed for event handlers and lifecycle methods.</p>
     * 
     * <p>The default implementation does nothing.</p>
     */
    @Override
    public void destroy() {
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected SessionBean1 getSessionBean1() {
        return (SessionBean1) getBean("SessionBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected RequestBean1 getRequestBean1() {
        return (RequestBean1) getBean("RequestBean1");
    }

    /**
     * <p>Return a reference to the scoped data bean.</p>
     *
     * @return reference to the scoped data bean
     */
    protected ApplicationBean1 getApplicationBean1() {
        return (ApplicationBean1) getBean("ApplicationBean1");
    }

    public String button1_action() {
        if (camposvacios())
            return mensaje(Mensajes.ERROR_CAMPOS_VACIOS);
        ValidacionDatosEntrada vde = new ValidacionDatosEntrada();
        String resultado;
        resultado= vde.nombreCorrecto(nombreTF.getText().toString().trim());
        if (!resultado.equals(Mensajes.ENTRADA_VALIDA))
            return mensaje(resultado);
        resultado= vde.correoCorrecto(correoOtroTF.getText().toString().trim());
        if(!resultado.equals(Mensajes.ENTRADA_VALIDA))
            return mensaje(resultado);
        resultado= vde.contrasenaCorrecta(contrasenaPF.getText().toString().trim());
        if(!resultado.equals(Mensajes.ENTRADA_VALIDA))
            return mensaje(resultado);
        resultado= vde.telefonoCorrecto(telefonoTF.getText().toString().trim());
        if(!resultado.equals(Mensajes.ENTRADA_VALIDA))
            return mensaje(resultado);
        if (!(nuevaContrasenaPF.isDisabled()&&cnuevaContrasenaPF.isDisabled())){
            resultado= vde.contrasenaCorrecta(nuevaContrasenaPF.getText().toString().trim());
            if(!resultado.equals(Mensajes.ENTRADA_VALIDA))
                return mensaje(resultado);
            resultado= vde.contrasenaCorrecta(cnuevaContrasenaPF.getText().toString().trim());
            if(!resultado.equals(Mensajes.ENTRADA_VALIDA))
                return mensaje(resultado);
        }
        if (!verificarContrasenasNuevas())
            return  mensaje(Mensajes.ERROR_CONTRASENAS);
        if (!verificarContrasenasActuales())
            return  mensaje(Mensajes.ERROR_CONTRASENAS);
        String nombre= nombreTF.getText().toString().trim();
        String correoOtro = correoOtroTF.getText().toString().trim();
        Long telefono = Long.parseLong(telefonoTF.getText().toString().trim());
        String dirección = direccionTF.getText().toString().trim();
        char[] contrasena;
        if (!(nuevaContrasenaPF.isDisabled()&&cnuevaContrasenaPF.isDisabled())){
            contrasena=nuevaContrasenaPF.getPassword().toString().trim().toCharArray();
        }
        contrasena=avo.getContrasena();

        AdministradorVO avoNuevo =new AdministradorVO(avo.getCedula(), nombre, contrasena, avo.getNombresala(), avo.getCorreoUnal(), correoOtro, dirección, telefono);
        resultado= fachada.editarAdministrador(avoNuevo);
        mensaje(resultado);
        return "/faces/paginas/AdministrarSala.jsp";
    }

    private String mensaje(String msm) {
        mensajeL.setText(msm);
        return null;
    }

    private boolean camposvacios() {
        try{
            String nombre=nombreTF.getText().toString().trim();
            nombreTF.setText(nombre);
            String correo=correoOtroTF.getText().toString().trim();
            correoOtroTF.setText(correo);
            String direccion=direccionTF.getText().toString().trim();
            direccionTF.setText(direccion);
            String telefono = telefonoTF.getText().toString().trim();
            telefonoTF.setText(telefono);
            String contrasena=contrasenaPF.getPassword().toString().trim();
            contrasenaPF.setPassword(contrasena);
            if (nuevaContrasenaPF.isDisabled()&&cnuevaContrasenaPF.isDisabled()){
                 String nuevaContrasena = nuevaContrasenaPF.getPassword().toString().trim();
                 String cNuevaContrasena = cnuevaContrasenaPF.getPassword().toString().trim();
                return nombre.equals("")||correo.equals("")||direccion.equals("")||telefono.equals("")||
                        nuevaContrasena.equals("")||cNuevaContrasena.equals("")||contrasena.equals("");
            }else{
                return nombre.equals("")||correo.equals("")||direccion.equals("")||telefono.equals("");
            }
        }catch(Exception e){
            return true;
        }
    }

    public String cambiarContrasenaB_action() {
        // TODO: Process the action. Return value is a navigation
        // case name where null will return to the same page.
        if (nuevaContrasenaPF.isDisabled()&&cnuevaContrasenaPF.isDisabled()){
            nuevaContrasenaPF.setDisabled(false);
            cnuevaContrasenaPF.setDisabled(false);
            cambiarContrasenaB.setText("No cambiar Contraseña");
        }else{
            nuevaContrasenaPF.setDisabled(true);
            cnuevaContrasenaPF.setDisabled(true);
            cambiarContrasenaB.setText("Cambiar Contraseña");
        }
        return null;
    }

    private boolean verificarContrasenasNuevas() {
        return nuevaContrasenaPF.getPassword().toString().trim().equals(cnuevaContrasenaPF.getPassword().toString().trim());
    }

    private boolean verificarContrasenasActuales() {
        return contrasenaPF.getPassword().toString().trim().equals(String.copyValueOf(avo.getContrasena()));
    }

    

}
